<?php

/**
 * User: nickbai
 * Date: 2017/10/23 13:33
 * Email: 1902822973@qq.com
 */

namespace app\admin\controller;

use app\admin\model\Admin;
use Illuminate\Support\Arr;
use app\admin\model\Service;

class Admins extends Base
{
    // 管理员列表
    public function index()
    {
        if (request()->isAjax()) {

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['user_name'] = ['like', '%' . $param['searchText'] . '%'];
            }

            $result = db('admins')->field(['ws_admins.*', 'ws_services.mobile'])->join('ws_services', 'ws_services.id = ws_admins.service_id', 'left')->where($where)->limit($offset, $limit)->select();
            foreach ($result as $key => $vo) {
                // 优化显示状态
                if (1 == $vo['status']) {
                    $result[$key]['status'] = '<span class="label label-primary">启用</span>';
                } else {
                    $result[$key]['status'] = '<span class="label label-danger">禁用</span>';
                }

                // 上次登录时间
                $result[$key]['last_login_time'] = empty($vo['last_login_time']) ? '' : date('Y-m-d H:i:s', $vo['last_login_time']);

                // 生成操作按钮
                if (1 != $vo['id']) {
                    $result[$key]['operate'] = $this->makeBtn($vo['id']);
                }
            }

            $return['total'] = db('admins')->where($where)->count();  //总数据
            $return['rows'] = $result;

            return json($return);
        }

        return $this->fetch();
    }

    // 添加管理员
    public function addAdmin()
    {
        if (request()->isPost()) {

            $param = input('post.');
            $hasAdmin = Admin::where('user_name', $param['user_name'])->value('id');
            if (!empty($hasAdmin)) {
                return json(['code' => -1, 'data' => '', 'msg' => '该管理员已经存在']);
            }
            $hasService = Service::where('user_name', $param['user_name'])->value('id');
            if (!empty($hasService)) {
                return json(['code' => -1, 'data' => '', 'msg' => '该客服已经存在']);
            }
            try {
                $admin = new Admin([
                    'user_name' => $param['user_name'],
                    'password' => md5($param['password'] . config('salt')),
                    'status' => $param['status']
                ]);
                $service = Service::create([
                    'user_name' => $admin->user_name,
                    'user_pwd' => $admin->password,
                    'user_avatar' => $param['user_avatar'],
                    'mobile' => $param['mobile'],
                    'status' => Service::STATUS_ENABLE,
                    'role' => Service::ROLE_SERVICE
                ]);
                $admin->service_id = $service->id;
                $admin->save();
            } catch (\Exception $e) {
                return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
            }

            return json(['code' => 1, 'data' => '', 'msg' => '添加管理员成功']);
        }

        $this->assign([
            'status' => config('kf_status')
        ]);

        return $this->fetch();
    }

    // 编辑管理员
    public function editAdmin()
    {
        if (request()->isAjax()) {

            $param = Arr::only(input('post.'), ['id', 'mobile', 'user_name', 'password', 'status']);

            // 检测用户修改的管理员是否重复
            $has = db('admins')->where('user_name', $param['user_name'])->where('id', '<>', $param['id'])->find();
            if (!empty($has)) {
                return json(['code' => -1, 'data' => '', 'msg' => '该管理员已经存在']);
            }

            // 修改用户密码
            if (empty($param['password'])) {
                unset($param['password']);
            } else {
                $param['password'] = md5($param['password'] . config('salt'));
            }

            try {
                db('admins')->where('id', $param['id'])->update($param);
            } catch (\Exception $e) {
                return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
            }

            return json(['code' => 1, 'data' => '', 'msg' => '编辑管理员成功']);
        }

        $id = input('param.id/d');
        $info = db('admins')->field(['ws_admins.*', 'ws_services.mobile', 'ws_services.user_avatar'])->join('ws_services', 'ws_services.id = ws_admins.service_id', 'left')->where('ws_admins.id', $id)->find();
        $this->assign([
            'info' => $info,
            'status' => config('kf_status')
        ]);
        return $this->fetch();
    }

    // 删除管理员
    public function delAdmin()
    {
        if (request()->isAjax()) {
            $id = input('param.id/d');

            try {
                $admin = Admin::findOrFail($id);
                $admin->service()->delete();
                $admin->delete();
            } catch (\Exception $e) {
                return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
            }

            return json(['code' => 1, 'data' => '', 'msg' => '删除客服成功']);
        }
    }

    // 生成按钮
    private function makeBtn($id)
    {
        $operate = '<a href="' . url('admins/editadmin', ['id' => $id]) . '">';
        $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';

        $operate .= '<a href="javascript:userDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
        $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';

        return $operate;
    }
}
